using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;

namespace ScorePoster
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class ScorePoster : System.Windows.Forms.Form
	{
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnPost;
		private System.Windows.Forms.TextBox txbUrl;
		landofshadows.FrisbeeGameService webService = new landofshadows.FrisbeeGameService();
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button btnBrowse;
		private System.Windows.Forms.TextBox txtFile;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ScorePoster()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			txbUrl.Text = webService.Url;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.label1 = new System.Windows.Forms.Label();
			this.btnPost = new System.Windows.Forms.Button();
			this.txbUrl = new System.Windows.Forms.TextBox();
			this.txtFile = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.btnBrowse = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 40);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(376, 80);
			this.label1.TabIndex = 0;
			this.label1.Text = "Shose a scorefile to post";
			// 
			// btnPost
			// 
			this.btnPost.Location = new System.Drawing.Point(328, 8);
			this.btnPost.Name = "btnPost";
			this.btnPost.Size = new System.Drawing.Size(56, 23);
			this.btnPost.TabIndex = 1;
			this.btnPost.Text = "Post";
			this.btnPost.Click += new System.EventHandler(this.btnPost_Click);
			// 
			// txbUrl
			// 
			this.txbUrl.BackColor = System.Drawing.SystemColors.ControlLight;
			this.txbUrl.Location = new System.Drawing.Point(105, 133);
			this.txbUrl.Name = "txbUrl";
			this.txbUrl.Size = new System.Drawing.Size(279, 20);
			this.txbUrl.TabIndex = 2;
			this.txbUrl.Text = "textBox1";
			// 
			// txtFile
			// 
			this.txtFile.Location = new System.Drawing.Point(64, 8);
			this.txtFile.Name = "txtFile";
			this.txtFile.Size = new System.Drawing.Size(192, 20);
			this.txtFile.TabIndex = 3;
			this.txtFile.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 11);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 4;
			this.label2.Text = "Score file:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 136);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(96, 23);
			this.label3.TabIndex = 5;
			this.label3.Text = "Web service URL:";
			// 
			// btnBrowse
			// 
			this.btnBrowse.Location = new System.Drawing.Point(264, 8);
			this.btnBrowse.Name = "btnBrowse";
			this.btnBrowse.Size = new System.Drawing.Size(56, 23);
			this.btnBrowse.TabIndex = 6;
			this.btnBrowse.Text = "Browse";
			this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
			// 
			// ScorePoster
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.LawnGreen;
			this.ClientSize = new System.Drawing.Size(392, 158);
			this.Controls.Add(this.btnBrowse);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtFile);
			this.Controls.Add(this.txbUrl);
			this.Controls.Add(this.btnPost);
			this.Controls.Add(this.label1);
			this.Name = "ScorePoster";
			this.Text = "DiscGolf Score poster";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new ScorePoster());
		}

		private void btnPost_Click(object sender, System.EventArgs e)
		{
			label1.Text = "Posting score...";
			try 
			{
				string fileName = openFileDialog1.FileName;
				XmlDocument doc = new XmlDocument();
				doc.Load(fileName);
				XmlElement root = doc.DocumentElement;
				//get course
				int courseId = int.Parse(root.Attributes["course"].Value);
				
				//get players	
				XmlNodeList playerNodes = root.SelectNodes("player");
				int[] player_ids = new int[playerNodes.Count];
				ArrayList scores = new ArrayList();
				int count = 0;
				foreach( XmlElement p in playerNodes ) 
				{
					player_ids[count] = int.Parse(p.Attributes["id"].Value);

					//get results
					XmlNodeList holeNodes = p.SelectNodes("hole");
					foreach( XmlElement h in holeNodes ) 
						scores.Add(int.Parse( h.Attributes["par"].Value ) );
					
					count ++;
				}

				int[] scores_int = (int[])scores.ToArray( typeof(int) );
				webService.Url = txbUrl.Text;
				webService.registerScore( courseId, player_ids, scores_int );
				label1.Text = "Posted OK";
			}
			catch (Exception ex ) 
			{
				label1.Text = ex.Message;
			}
		
		}

		private void btnBrowse_Click(object sender, System.EventArgs e)
		{
			try 
			{
				if( openFileDialog1.ShowDialog() == DialogResult.OK ) 
				{
					txtFile.Text = openFileDialog1.FileName;
					label1.Text = "Ready to post.";
				}
				else 
				{
					Dispose();
				}
			}				
			catch (Exception ex ) 
			{
				label1.Text = ex.Message;
			}
		
		}
	}
}
